# -*- coding:utf-8 -*-
#作者：琴弦上的宇宙
#时间：2021年10月22日

import os
from common import *
from openCv import *

class scTaskFinish(myScene):
    def __init__(self,bValid):
        self.bValid=bValid
        self.quitPos=[0,0]
        self.name='scTaskFinish'
        self.path='resource/mercenary/base/scTaskFinish'
        self.tagPng=[myPng(self.path,png) for png in os.listdir(self.path) if png.startswith('tag')]
        self.funcPng=[myPng(self.path,png) for png in os.listdir(self.path) if png.startswith('func')]

    def resetName(self,newName):
        self.name=newName

    def enable(self):
        self.bValid=True

    def dis(self):
        self.bValid=False

    def isOwn(self,background):
        if self.bValid:
            tagList={}
            for tag in self.tagPng:
                bFind,x,y,w,h=bFindInBackground(background,tag,0.90)
                if bFind:
                    tagList[tag.name]=(x,y,w,h)
            if ('tagFire' in tagList):
                pos=tagList['tagFire']
                self.quitPos[0]=pos[0]+w/2
                self.quitPos[1]=pos[1]-50
                return True
            else:
                return False
        else:
            return False

    def proc(self,background):

        #循环
        bg=background
        runTime=time.time()
        while True:

            #检测
            for func in self.funcPng:
                if func.name=='funcFinish':
                    bFind,x,y,w,h=bFindInBackground(bg,func,0.70)
                    if bFind:
                        self.resetName('scTaskFinish_Finished')
                        SaveTaskPng()
                        moveAndClick(x+w/2,y+h/2)
                        bg=SaveScreen()
                        if not self.isOwn(bg):
                            MyGui.gTaskFinishCnt+=1
                        return True

            #检测任务
            bFindTask=False
            for func in self.funcPng:
                if (func.name=='funcTask18') or \
                    (func.name=='funcTask13')or \
                    (func.name=='funcTask7')or \
                    (func.name=='funcTask15') or \
                    (func.name=='funcTask5'):
                    bFind,x,y,w,h=bFindInBackground(bg,func,0.75)
                    if bFind:
                        bFindTask=True
                        if not MyGui.bTaskRunning:
                            self.resetName('scTaskFinish_Task')
                            MyGui.gRound=555
                            MyGui.bTaskLevel=True
                            MyGui.bTaskRunning=True
                            if func.name=='funcTask18':MyGui.gTaskDifficulty='2'
                            elif func.name=='funcTask13':MyGui.gTaskDifficulty='2'
                            elif func.name=='funcTask7':MyGui.gTaskDifficulty='2'
                            elif func.name=='funcTask15':MyGui.gTaskDifficulty='1'
                            elif func.name=='funcTask5':MyGui.gTaskDifficulty='1'
                            moveAndClick(self.quitPos[0],self.quitPos[1])
                            return True

            #未检测到任务5，7等
            if not bFindTask:
                if MyGui.bTaskRunning:
                    MyGui.gRound=555
                    MyGui.bTaskLevel=True
                    MyGui.bTaskRunning=False

            #超时
            if ((time.time()-runTime)>2):
                self.resetName('scTaskFinish_UnFinished')
                moveAndClick(self.quitPos[0],self.quitPos[1])
                return True

            #重新截图判断
            bg=SaveScreen()
            if not self.isOwn(bg):
                return False
